
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // stations: "",
    inputShowed: false,
    inputVal: "",
    cityResult: false
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },

  inputTyping: function (e) {
    var val = e.detail.value.toUpperCase(),
      stations = this.data.stations,
      cityRearch = [];
    if (val.length > 0) {
      for (var item in stations) {
        stations[item].show = true;
        for (var city in stations[item].StationList) {
          if (stations[item].StationList[city].StationName.indexOf(val) == 0 || stations[item].StationList[city].StationPYCode.indexOf(val) == 0) {
            stations[item].StationList[city].show = true;
            cityRearch.push(stations[item].StationList[city]);
          } else {
            stations[item].StationList[city].show = false
          }
        }
      }
      this.setData({
        cityResult: true,
        cityRearch: cityRearch,
      })

    } else {
      this.setData({
        cityResult: false
      })
    }

    this.setData({
      stations: stations
    })
  },

  code: function (e) {
    var index = e.currentTarget.dataset.code;
    var stationName = this.data.stations;
    if(stationName != "" && stationName != null && typeof(stationName) != 'undefined'){
      for (var i = 0; i < stationName.length; i++) {
        if (stationName[i].CityCode == index) {
          for (var k = 0; k < stationName[i].StationList.length; k++) {
            stationName[i].StationList[k].show = stationName[i].StationList[k].show == true ? false : true;
          }
        } else {
          for (var k = 0; k < stationName[i].StationList.length; k++) {
            stationName[i].StationList[k].show = false;
          }
        }
      }
    }
    
    this.setData({
      stations: stationName
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.StationList();
  },
  StationList: function () {
    app.showLoadings();
    var that = this;
    wx.request({
      url: app.serviceurl + 'StationLine/QueryStations',
      
      success: function (res) {
        if (res.data.Result == true) {
          if (res.data.CityList == null || typeof (res.data.CityList) == 'undefined') {
            return;
          }
          for (var item in res.data.CityList) {
            for (var city in res.data.CityList[item].StationList) {
              res.data.CityList[item].StationList[city].show = false;
              res.data.CityList[item].StationList[city].hidden = true;
            }
          }
          res.data.CityList[0].show = true;
          for (var i in res.data.CityList[0].StationList) {
            res.data.CityList[0].StationList[i].show = true;
          }
          that.setData({
            stations: res.data.CityList,
          });
        }
      },
      fail: function (xhr) {
        console.log(xhr);
      },
      complete: function () {
        app.hideLoadings();
        that.setData({ placeholder: '中文搜索'})
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },
  selectstation: function (event) {
    var globalcode = event.currentTarget.dataset.globalcode;
    //app.stationName = event.currentTarget.dataset.stationname;
    wx.navigateTo({
      url: '/pages/queryDetail/queryDetail?globalcode=' + globalcode
    });
  }
})